﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LetiLib.Device;

namespace LedCenter.GUI
{
    public partial class UpdateProgress : Form
    {
        public UpdateProgress()
        {
            InitializeComponent();
            
        }

        private List<TCPDevice> deviceList = new List<TCPDevice>();

        private void UpdateProgress_Load(object sender, EventArgs e)
        {
            return;
            // 
            // deviceProgressControl1
            // 
            DeviceProgressControl dpc = new DeviceProgressControl();
            int progressWidth = progressPanel.ClientSize.Width - 
                System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
            dpc.Location = new System.Drawing.Point(0, 0);
            dpc.Margin = new System.Windows.Forms.Padding(0);
            dpc.setInfo("dpc1");
            dpc.Size = new System.Drawing.Size(progressWidth, 64);
            dpc.TabIndex = 0;
            this.progressPanel.Controls.Add(dpc);
            dpc = new DeviceProgressControl();
            dpc.Location = new System.Drawing.Point(0, 0);
            dpc.Margin = new System.Windows.Forms.Padding(0);
            dpc.setInfo("dpc2");
            dpc.Size = new System.Drawing.Size(progressWidth, 64);
            dpc.TabIndex = 0;
            this.progressPanel.Controls.Add(dpc);
            dpc = new DeviceProgressControl();
            dpc.Location = new System.Drawing.Point(0, 0);
            dpc.Margin = new System.Windows.Forms.Padding(0);
            dpc.setInfo("dpc3");
            dpc.Size = new System.Drawing.Size(progressWidth, 64);
            dpc.TabIndex = 0;
            this.progressPanel.Controls.Add(dpc);
            dpc = new DeviceProgressControl();
            dpc.Location = new System.Drawing.Point(0, 0);
            dpc.Margin = new System.Windows.Forms.Padding(0);
            dpc.setInfo("dpc4");
            dpc.Size = new System.Drawing.Size(progressWidth, 64);
            dpc.TabIndex = 0;
            this.progressPanel.Controls.Add(dpc);
            dpc = new DeviceProgressControl();
            dpc.Location = new System.Drawing.Point(0, 0);
            dpc.Margin = new System.Windows.Forms.Padding(0);
            dpc.setInfo("dpc5");
            dpc.Size = new System.Drawing.Size(progressWidth, 64);
            dpc.TabIndex = 0;
            this.progressPanel.Controls.Add(dpc);
        }

        public void Add(TCPDevice dev)
        {
            if (deviceList.Contains(dev))
            {
                return;
            }
            deviceList.Add(dev);

            DeviceProgressControl dpc = new DeviceProgressControl();
            int progressWidth = progressPanel.ClientSize.Width -
                System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
            dpc.Location = new System.Drawing.Point(0, 0);
            dpc.Margin = new System.Windows.Forms.Padding(0);
            dpc.Name = dev.Name;
            dpc.setInfo(dev.Name);
            dpc.Size = new System.Drawing.Size(progressWidth, 64);
            dpc.TabIndex = 0;

            dev.bwSimulator.ProgressChanged += new ProgressChangedEventHandler
                    (dpc.ProgressChanged);
            dev.bwSimulator.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                    (dpc.ProgressCompleted);
            dpc.OnStoped += new OnStopHandler(OnStoped);
            this.progressPanel.Controls.Add(dpc);

            this.Invalidate();
        }

        void OnStoped(string name)
        {
            foreach (TCPDevice dev in deviceList)
            {
                if (dev.Name.Equals(name))
                    dev.stopProgramSim();
            }
        }

        private void btnCancelAll_Click(object sender, EventArgs e)
        {
            foreach (TCPDevice dev in deviceList)
            {
                dev.stopProgramSim();
            }
        }

        private void UpdateProgress_Resize(object sender, EventArgs e)
        {
            //return;
            this.Width = 400; // fix width
            if (this.Height > Screen.FromControl(this).Bounds.Height)
                this.Height = Screen.FromControl(this).Bounds.Height;
            if (this.Height < 350)
                this.Height = 350;

            Invalidate(true);
        }
        /*
        bool mouseEntered = false;
        private void progressPanel_MouseEnter(object sender, EventArgs e)
        {
            if (mouseEntered) return;
            mouseEntered = true;
            
            progressPanel.Focus();
        }

        private void progressPanel_MouseLeave(object sender, EventArgs e)
        {
            mouseEntered = false;
            this.ActiveControl = null;
        }
        */
        private void UpdateProgress_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (TCPDevice dev in deviceList)
            {
                dev.stopProgramSim();
            }
        }
    }
}
